package com.medusa.aps.business.util;

import com.medusa.aps.business.model.constant.DigitConstants;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * <p>车间统计Utils</p>
 */
public interface WorkshopStatUtils {

    /**
     * 计算当日计划数量
     *
     * @param plannedQuantity 工序计划数量
     * @param totalMinutes    总耗时
     * @param requiredMinutes 当前所需时间(以分钟为单位)
     * @return
     */
    static BigDecimal calculatePlannedCnt(BigDecimal plannedQuantity, Long totalMinutes, Long requiredMinutes) {
        return plannedQuantity
                .multiply(new BigDecimal(requiredMinutes))
                .divide(new BigDecimal(totalMinutes), 0, RoundingMode.CEILING);
    }

    /**
     * 计算生产进度
     *
     * @param completedCnt
     * @param plannedCnt
     * @return
     */
    static BigDecimal calculateProductionSchedule(BigDecimal completedCnt, BigDecimal plannedCnt) {
        if (plannedCnt.compareTo(new BigDecimal(0)) == 0) {
            return new BigDecimal(0);
        }
        BigDecimal percentage = completedCnt.divide(plannedCnt, 4, RoundingMode.CEILING).multiply(new BigDecimal(100));
        if (percentage.compareTo(DigitConstants.MAX_PROCESS_SCHEDULE) > 0) {
            percentage = DigitConstants.MAX_PROCESS_SCHEDULE;
        } else if (percentage.compareTo(BigDecimal.ZERO) <= 0) {
            percentage = new BigDecimal(0);
        }
        return percentage;
    }
}
